package com.tanhua.dubbo.server.api;

import com.alibaba.dubbo.config.annotation.Service;
import com.tanhua.dubbo.server.pojo.Question;
import com.tanhua.dubbo.server.pojo.Report;
import com.tanhua.dubbo.server.pojo.Soul;
import org.bson.types.ObjectId;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;
import org.springframework.data.mongodb.core.query.Update;

import java.util.ArrayList;
import java.util.List;

@Service(version = "1.0.0")
public class SoulApiImpl implements SoulApi {

    @Autowired
    private MongoTemplate mongoTemplate;

    /**
     *测灵魂-问卷列表
     * @param userId
     * @return
     */

    @Override
    public List<Soul> querySoulList(Long userId) {
        Query query = Query.query(Criteria.where("userId").is(userId));
        List<Soul> soulList = mongoTemplate.find(query, Soul.class);
        return soulList;
    }

    /**
     *测灵魂-问卷列表-题目及选项
     * @param questionIds
     * @return
     */
    @Override
    public List<Question> queryQuestionList(List<ObjectId> questionIds) {
        Query query = Query.query(Criteria.where("id").in(questionIds));
        List<Question> questionList = mongoTemplate.find(query, Question.class);
        return questionList;
    }

    /**
     * 测灵魂-问卷列表-保存结果
     */
    @Override
    public String saveReport(Report report) {

        Query query2 = Query.query(Criteria.where("soulId").is(report.getSoulId()));
        Report oldReport = mongoTemplate.findOne(query2, Report.class);
        if (null != oldReport){
            //如果已经存在 则进行更新
            Update update = new Update();
            update.set("conclusion",report.getConclusion());
            update.set("cover",report.getCover());
            update.set("dimensions",report.getDimensions());
            mongoTemplate.updateFirst(query2,update,Report.class);

            return oldReport.getId().toHexString();
        }

        report.setId(new ObjectId());
        mongoTemplate.save(report);
        //开放下套试题
        Query query = Query.query(Criteria.where("id").is(report.getSoulId()));
        Soul soul = mongoTemplate.findOne(query, Soul.class);
        Update update = new Update();
        update.set("isLock",0);
        Query query1 = null;
        if (soul.getStar() == 2){
            query1 = Query.query(Criteria.where("userId").is(report.getUserId()).and("star").is(3));
            mongoTemplate.updateFirst(query1,update,Soul.class);
        }else if (soul.getStar() == 3){
            query1 = Query.query(Criteria.where("userId").is(report.getUserId()).and("star").is(5));
            mongoTemplate.updateFirst(query1,update,Soul.class);
        }

        return report.getId().toHexString();
    }

    /**
     * 查询结果报告
     * @param reportId
     * @return
     */
    @Override
    public Report queryReport(String reportId) {
        Query query = Query.query(Criteria.where("id").is(new ObjectId(reportId)));
        return mongoTemplate.findOne(query,Report.class);
    }

    /**
     * 根据 测试题的 id 去查 结果的 id
     */
    @Override
    public String queryReportId(String soulId) {
        Query query = Query.query(Criteria.where("soulId").is(new ObjectId(soulId)));
        Report report = mongoTemplate.findOne(query, Report.class);
        if (null == report){
            return null;
        }
        return report.getId().toHexString();
    }

    /**
     * 根据 题目id 查询题目
     * @param questionId
     * @return
     */
    @Override
    public Question queryQuestion(String questionId) {
        Query query = Query.query(Criteria.where("id").is(new ObjectId(questionId)));
        return mongoTemplate.findOne(query,Question.class);
    }

    /**
     * 通过题目 level 和userId 拿到 对应的问卷id
     * @return
     */
    @Override
    public String querySoulId(String level, Long userId) {

        Query query = Query.query(Criteria.where("userId").is(userId).and("level").is(level));
        Soul soul = mongoTemplate.findOne(query, Soul.class);
        if (null == soul){
            return null;
        }
        return soul.getId().toHexString();
    }

    /**
     * 查询与你类型相同的人
     * @return
     */
    @Override
    public List<Report> querySimilarYou(Report report) {

        Soul soul = mongoTemplate.findOne( Query.query(Criteria.where("id").is(report.getSoulId())), Soul.class);

        List<Report> reportList = mongoTemplate.find(new Query(), Report.class);
        ArrayList<ObjectId> soulIds = new ArrayList<>();
        for (Report report1 : reportList) {
            soulIds.add(report1.getSoulId());
        }

        Query query1 = Query.query(Criteria.where("id").in(soulIds).and("star").is(soul.getStar()));
        List<Soul> soulList = mongoTemplate.find(query1, Soul.class);
        soulIds.clear();
        for (Soul soul1 : soulList) {
            soulIds.add(soul1.getId());
        }

        Query query = Query.query(Criteria.where("conclusion").is(report.getConclusion()).and("soulId").in(soulIds));
        return mongoTemplate.find(query,Report.class);
    }

    /**
     * 根据type获取题目
     */
    @Override
    public List<Question> queryQuestions(Integer type) {
        Query query = Query.query(Criteria.where("type").is(type));
        return mongoTemplate.find(query,Question.class);
    }

    /**
     * 保存 soul
     */
    @Override
    public String saveSoul(Soul soul) {
        mongoTemplate.save(soul);
        return soul.getId().toHexString();
    }
}
